import request from '@/utils/request'
import website from "@/const/website";
const scope = 'server'
// 获取 授权链接
export function getAuthUrl(data) {
    return request({
        url: '/mp-service/auth/index',
        method: 'post',
        data:data
    })
}

//获取微信用户信息
export function getAuthUser(obj) {
    const grant_type = 'wechat_mp_code';
    let basicAuth = 'Basic ' + window.btoa(website.formLoginClient);
    obj.grant_type = grant_type;
    obj.scope = scope;
    return request({
        url: '/oauth-server/oauth2/token',
        headers: {
            isToken: false,
            Authorization: basicAuth
        },
        method: 'post',
        params:obj
    })
}

export function checkToken(token){
    let basicAuth = 'Basic ' + window.btoa(website.formLoginClient);
    return request({
        url: '/oauth-server/oauth2/check_token',
        headers: {
            isToken: false,
            Authorization: basicAuth
        },
        method: 'post',
        params: {token}
    })
}

export const refreshToken = refresh_token => {
    const grant_type = 'refresh_token'
    // 获取当前选中的 basic 认证信息
    let basicAuth = 'Basic ' + window.btoa(website.formLoginClient);
    return request({
        url: '/oauth-server/oauth2/token',
        headers: {
            isToken: false,
            Authorization: basicAuth
        },
        method: 'post',
        params: {refresh_token, grant_type, scope}
    })
}

// 登录，返回用户详情
export function getUserInfo() {
    return request({
        url: '/mp-service/auth/info',
        method: 'get'
    })
}

//退出
export function logout() {
    return request({
        url: '/oauth-server/oauth2/logout',
        method: 'delete'
    })
}
